import json

import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import random
from config import get_config

print("Begin testing...")
driver = webdriver.Chrome()
url = "https://www.jd.com"
driver.maximize_window()
driver.implicitly_wait(2)
# 访问网站，清空旧cookies信息
driver.get(url)
time.sleep(5)
driver.delete_all_cookies()

# 加载 cookies信息
with open("cookies.txt", "r") as f:
    cookies = json.load(f)
for cookie in cookies:
    driver.add_cookie(cookie)

# 验证是否登录成功
driver.get(url)
time.sleep(3)

# 通过id=key定位到搜索框
input_search = driver.find_element(By.ID, 'key')
# 在输入框中输入“手机”
input_search.send_keys('手机')
# 模拟键盘回车Enter操作进行搜索
input_search.send_keys(Keys.ENTER)
# 强制等待3秒
time.sleep(3)


# 函数：滚动页面并获取商品的data-spu属性
def get_data_spu():
    SCROLL_DURATION = 40  # 滚动持续时间（秒）
    start_time = time.time()

    while True:
        # 生成随机的滑动距离
        scroll_distance = random.randint(200, 800)
        # 将页面向下滚动一个随机距离
        driver.execute_script("window.scrollBy(0, arguments[0]);", scroll_distance)
        # 等待加载
        time.sleep(0.5)  # 固定的等待时间
        # 检查是否已经到达预定的滚动时间
        current_time = time.time()
        if current_time - start_time > SCROLL_DURATION:
            break

        # 检查是否有加载失败的提示
        try:
            retry_link = driver.find_element(By.XPATH,
                                             '//span[contains(text(), "加载失败，请")]/a[contains(text(), "重试")]')
            retry_link.click()
            time.sleep(3)  # 等待页面加载完成
        except:
            pass

    # 获取商品列表中的所有li标签
    goods_list = wait.until(EC.presence_of_element_located((By.ID, 'J_goodsList')))
    li_elements = goods_list.find_elements(By.XPATH, './/li[@data-sku]')

    # 存储每个li标签中的data-spu属性
    data_spu_list = [li.get_attribute('data-sku') for li in li_elements]
    return data_spu_list


# 获取前N页的data-spu
def get_multiple_pages_data_spu(num_pages):
    all_data_spu = []
    for page in range(num_pages):
        # 获取当前页的data-spu
        data_spu_list = get_data_spu()
        all_data_spu.extend(data_spu_list)

        if page < num_pages - 1:  # 如果不是最后一页，点击下一页按钮
            next_page_button = driver.find_element(By.CSS_SELECTOR, 'a.pn-next')
            next_page_button.click()
            time.sleep(3)  # 等待页面加载完成

    return all_data_spu


# 获取前三页的data-spu
data_spu_list = get_multiple_pages_data_spu(2)
# 打印获取到的data-spu列表
print(len(data_spu_list))
# 去重并去除空值
data_spu_list = list(filter(None, set(data_spu_list)))

# 转换为Excel文件
df = pd.DataFrame(data_spu_list, columns=['data_spu'])
df.to_csv('../data_spu_list.csv', index=False)
